package com.small.mybatis.boot.spring;

import com.small.mybatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;

import javax.annotation.Resource;

/**
 * @Author zhoujin
 * @Date 2022/6/18 16:18
 */
public class MapperFactoryBean<T> implements FactoryBean<T> {

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    private Class<T> mapperClass;

    public MapperFactoryBean( Class<T> mapperClass) {
        this.mapperClass = mapperClass;
    }

    /**
     * 创建mapper的代理对象
     * @return
     * @throws Exception
     */
    @Override
    public T getObject() throws Exception {
        return sqlSessionFactory.openSession().getMapper(mapperClass);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
